#include<QObject>
#include<QTest>

#include"Database.h"
#include"Song.h"

class TestDatabase: public QObject{
Q_OBJECT
private:
	Database *m_db;
	bool m_slotCalled;
private slots:
	void initTestCase(){
		m_db = new Database(0);
		m_slotCalled = false;
	}
	
	void cleanupTestCase(){
		if(m_db)
			delete m_db;
	}
	
	void testConsistency(){
		QList<QString> artists = m_db->getArtists();
		for(int i = 0; i < artists.size(); i++){
			QList<QString> albums = m_db->getAlbums(artists[i]);
			QVERIFY(albums.size());
			for(int j = 0; j < albums.size(); j++){
				QVERIFY( m_db->getSongs(artists[i], albums[j]).size() );
			}
		}
	}
	
	void testAdding(){
		try{
			qApp->setApplicationName("test");
			Song *song = new Song("song.ogg");
			connect(m_db, SIGNAL(contentsChanged(QSet<QString>)), this, SLOT(testAdding2()));
			m_db->addEntry(song);
			delete song;
			int counter = 0;
			while(!m_slotCalled && counter < 30){//For a maximum of 30 * 500 = 15000 = 15 seconds.
				counter++;
				QTest::qWait(500);
			}
		}catch(...){
			QVERIFY(0);
		}
	}
public slots:
	void testAdding2(){
		printf("testAdding2\n");
		m_slotCalled = true;
		m_db->disconnect(this, SLOT(testAdding2()));
		try{
			Song *song = new Song("song.ogg");
			QList<QString> songs = m_db->getSongs(song->artist(), song->album());
			m_db->removeEntry("song.ogg");
			bool found = false;
			for(int i = 0; i < songs.size() && !found; i++)
				found = songs[i].endsWith(song->path());
			delete song;
			QVERIFY(found);
		}catch(...){
			QVERIFY(0);
		}
	}
};

QTEST_MAIN(TestDatabase);
#include"TestZDatabase.moc"